package com.armani.service.impl;


import com.alibaba.fastjson.JSON;
import com.armani.mapper.ImgMapper;
import com.armani.mapper.SkuMapper;
import com.armani.mapper.SpuMapper;
import com.armani.model.ImgModel;
import com.armani.model.ItemModel;
import com.armani.model.SkuModel;
import com.armani.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;



/**
 * @ProjectName: armani
 * @Package: com.armani.service.impl
 * @ClassName: ItemServiceImpl
 * @Description: 商品详情接口实现类
 * @Author: lenovo
 * @Date: 2021/12/3 0:19
 * @Version: 1.0
 **/
@Service
public class ItemServiceImpl implements ItemService {
    @Autowired
    private SpuMapper spuMapper;
    @Autowired
    private SkuMapper skuMapper;
    @Autowired
    private ImgMapper imgMapper;

    /**
     * 查询商品详情列表
     *
     * @param params 筛选条件参数
     * @return 商品详情列表集合
     */
    @Override
    public List<ItemModel> itemList(Map<String, Object> params) {
        List<ItemModel> itemList = spuMapper.selectAll(params);
        for (ItemModel item : itemList) {
            item.setSkuList(skuMapper.searchBySpuId(item.getId()));
            List<SkuModel> skuList=item.getSkuList();
            for (SkuModel sku:skuList){
                sku.setImgList(imgMapper.searchBySkuId(sku.getId()));
            }
        }
        return itemList;
    }

    /**
     * 获取总条数
     *
     * @param params 筛选条件参数
     * @return 总条数
     */
    @Override
    public Integer count(Map<String, Object> params) {
        return spuMapper.count(params);
    }


    /**
     * 添加商品信息
     *
     * @param itemModel 商品信息参数对象
     * @return 影响行数
     */
    @Override
    public Integer itemAdd(ItemModel itemModel) {
        spuMapper.insertSpu(itemModel);
        Integer spuId=itemModel.getId();
        List<SkuModel> skuList=itemModel.getSkuList();
        for (SkuModel sku:skuList){
            sku.setSpuId(spuId);
            skuMapper.insertSku(sku);
            Integer skuId=sku.getId();
            List<ImgModel> imgList=sku.getImgList();
            for (ImgModel img:imgList){
                img.setSkuId(skuId);
                imgMapper.insertImg(img);
            }
        }
        return 1;
    }


}
